import smtplib
from email.mime.application import MIMEApplication
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

# 邮件服务器配置
smtp_server = 'smtp.qq.com'
port = 587  # 或者465，取决于服务器要求
username = '1271709068@qq.com'
password = 'vzolqpsqqjuufjia'


def send(receiver_email: str, subject: str, body: str, file_name: str):
    # 连接到SMTP服务器
    server = smtplib.SMTP(smtp_server, port)
    server.starttls()  # 启用TLS
    server.login(username, password)

    sender_email = '1271709068@qq.com'  # 发件人

    # 创建邮件对象和MIMEMultipart对象
    msg = MIMEMultipart()
    msg['From'] = Header(sender_email)
    msg['Subject'] = Header(subject, 'utf-8')  # 邮件主题
    msg['To'] = Header(receiver_email)  # 收件人
    # 添加邮件正文
    msg.attach(MIMEText(body, 'plain', 'utf-8'))

    # 添加附件
    att = MIMEApplication(open(file_name, 'rb').read())
    att.add_header('Content-Disposition', 'attachment', filename=file_name.split('\\')[-1])
    msg.attach(att)

    server.sendmail(sender_email, receiver_email, msg.as_string())
    # 关闭服务器连接
    server.quit()
